package com.zzw.tddl;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import java.text.MessageFormat;
import java.util.List;

/**
 * 描述
 *
 * @author zhulixin
 */
public class AucsInsertSingleMain {

    public static void main(String[] args) throws Exception {
        System.setProperty("nacos.endpoint", "midenv.to5.mychebao.com");
        //ConfigService configService = NacosFactory.createConfigService();

        String app = "lms";
        String host = "188.188.23.152";
        String port = "3306";
        String userName = "root";
        String encPasswd = "54f937ccb823c094d1643f81ebcd1be4";

        // 保存groups
        String dataId = new MessageFormat("com.taobao.tddl.v1_{0}_dbgroups").format(new Object[]{app});
        // 逗号分隔
        String groups = app + "_main";
        String content = groups;
        /*boolean result = configService.publishConfig(dataId, null, content);
        System.out.println("save group list:" + result);*/

        String[] groupList = groups.split(",");
        int groupId = 0;
        String[] dbNames = {"lms"};
        for (String groupName : groupList) {
            // 逗号分隔
            String atoms = groupName + "_atom0";
            String rWeights = "10";
            String wWeights = "10";
            String pWeights = "0";

            String[] atomList = atoms.split(",");
            String[] rWeightList = rWeights.split(",");
            String[] wWeightList = wWeights.split(",");
            String[] pWeightList = pWeights.split(",");

            List<String> contentList = Lists.newArrayList();
            for (int i = 0; i < atomList.length; i++) {
                String atom = atomList[i];
                String rWeight = rWeightList[i];
                String wWeight = wWeightList[i];
                String pWeight = pWeightList[i];
                contentList.add(atom + ":" + "r" + rWeight + "w" + wWeight + "p" + pWeight);
            }

            // 保存atom
            dataId = new MessageFormat("com.taobao.tddl.jdbc.group_V2.4.1_{0}").format(new Object[]{groupName});
            content = Joiner.on(",").join(contentList);
          /*  result = configService.publishConfig(dataId, null, content);
            System.out.println("save atom list:" + result);*/

            // 保存group对应app配置信息
            dataId = new MessageFormat("com.taobao.tddl.jdbc.extra_config.group_V2.4.1_{0}.{1}")
                    .format(new Object[]{groupName, app});
            content = "{\"sqlForbid\":[],\"sqlDsIndex\":{},\"tabDsIndex\":{},\"defaultMain\":\"true\"}";

            String dbName = dbNames[groupId];
            for (String atom : atomList) {
                // 保存数据库信息
//                    String dbName = "test_cdal_0" + groupId;
                dataId = new MessageFormat("com.taobao.tddl.atom.global.{0}").format(new Object[]{atom});
                content = "ip=" + host + "\n" +
                        "port=" + port + "\n" +
                        "dbName=" + dbName + "\n" +
                        "dbType=mysql\n" +
                        "dbStatus=RW";
                // 保存数据库属性信息
                dataId = new MessageFormat("com.taobao.tddl.atom.app.{0}.{1}").format(new Object[]{app, atom});
                content = "userName=" + userName + "\n" +
                        "minPoolSize=20\n" +
                        "maxPoolSize=100\n" +
                        "idleTimeout=2\n" +
                        "blockingTimeout=10000\n" +
                        "preparedStatementCacheSize=1000\n" +
                        "connectionProperties=useUnicode=true;characterEncoding=UTF-8;zeroDateTimeBehavior=convertToNull;tinyInt1isBit=false\n" +
                        "readRestrictTimes=0\n" +
                        "writeRestrictTimes=0\n" +
                        "timeSliceInMillis=0";
                /*result = configService.publishConfig(dataId, null, content);
                System.out.println("save db property:" + result);
*/
                // 保存数据库密码
                dataId = new MessageFormat("com.taobao.tddl.atom.passwd.{0}.{1}.{2}")
                        .format(new Object[]{dbName, "mysql", userName});
                content = "encPasswd=" + encPasswd;
              /*  result = configService.publishConfig(dataId, null, content);
                System.out.println("save db password:" + result);*/
            }
            groupId++;
        }
    }
}
